package classes;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Log {
	private File error;
	private File success;
	private String combined=AdminFunctions.defaultdirectory+"\\"+AdminFunctions.IWfoldername+"\\"+AdminFunctions.rootfoldername;
	public Log(){
	
	}
	
	public void writeIntoSuccessLog(String email) throws IOException{///put transaction object and email of user in the arguments
		checkIWFolderExistence();
		checkRootFolderExistence();
		checkUsersFolderExistence(email);
		checkSuccessFileExistence(email);
		FileWriter fw = new FileWriter(success.getAbsoluteFile(),true);
		BufferedWriter bw = new BufferedWriter(fw);

			bw.write("success written71deepaksandeepgaurakuchbhinahihae");
			

			bw.close();
		
	}
	public void writeIntoErrorLog(String email) throws IOException{///put Exception object with email
		checkIWFolderExistence();
		checkRootFolderExistence();
		checkUsersFolderExistence(email);
		checkErrorFileExistence(email);
		FileWriter fw = new FileWriter(error.getAbsoluteFile(),true);
	BufferedWriter bw = new BufferedWriter(fw);

		bw.write("error written71fdgffddf");
		

		bw.close();
	}
	private void checkUsersFolderExistence(String email){//put user's email id in the arguments
		 File f=new File(combined+"\\"+email);
		 if(f.exists()){
			 
		 }
		 else{
			 f.mkdir();
		 }
		 
	}
	private void checkErrorFileExistence(String email) throws IOException{
		File f=new File(combined+"\\"+email+"\\"+"errors.txt");
		if(f.isFile()){
			///file already present
			System.out.println("file present1");
		}
		else{
			f.createNewFile();
			System.out.println("new file created1");
		}
		error=f;
		
	}
	private void checkSuccessFileExistence(String email) throws IOException{
		File f=new File(combined+"\\"+email+"\\"+"success.txt");
		if(f.isFile()){
			///file already present
		}
		else{
			f.createNewFile();
		}
		success=f;
	}
	private void checkRootFolderExistence(){
		File f=new File(combined);
		if(f.exists()){
			
		}
		else{
			f.mkdir();
		}
		
	}
	private void checkIWFolderExistence(){
		File f=new File(AdminFunctions.defaultdirectory+"\\"+AdminFunctions.IWfoldername);
		if(f.exists()){
			
		}
		else{
			f.mkdir();
		}
	}
	public void writeIntoCSV(ResultSet rsmetadata,ResultSet rsdata) throws IOException, SQLException{// pass the result set object here
		checkIWFolderExistence();
		File f=checkCSVFolderExistence();
		SimpleDateFormat s=new SimpleDateFormat("dd-MM-yyyy");
		Date now=new Date();
		String dd=s.format(now);
		File f1=new File(f.getPath()+"\\"+dd+".csv");
		if(f1.isFile()){
			
			
		}
		else{
			
			f1.createNewFile();
			
		}
		FileWriter fw=new FileWriter(f1.getAbsoluteFile());
		BufferedWriter bw=new BufferedWriter(fw);
		//System.out.println(rsmetadata.getString("COLUMN_NAME"));
		int rsmetadatacounter=1;
		while(rsmetadata.next()){
			System.out.println();
			bw.write(rsmetadata.getString("COLUMN_NAME"));
			if(rsmetadatacounter!=rsdata.getMetaData().getColumnCount())
			bw.write(",");
			rsmetadatacounter++;
		}
		bw.write("\n");
		int rsdatacounter;
		while(rsdata.next()){
			rsdatacounter=1;
			for(int i=1;i<=rsdata.getMetaData().getColumnCount();i++){
				bw.write(rsdata.getString(i));
				if(rsdatacounter!=rsdata.getMetaData().getColumnCount())
				bw.write(",");
				rsdatacounter++;
			}
			bw.write("\n");
		}
		
		
		
		bw.close();
		//////mail the file 
		//File f=new File(AdminFunctions.defaultdirectory+"\\"+AdminFunctions.rootfoldername);
		
	}
	private File checkCSVFolderExistence(){
		File f=new File(AdminFunctions.defaultdirectory+"\\"+AdminFunctions.IWfoldername+"\\"+AdminFunctions.csvfolder);
		if(f.exists()){
			
		}
		else{
			f.mkdir();
		}
		return f;
	}
}
